package com.bizmerce.ebms.engine;

import java.io.FileNotFoundException;
import java.util.Iterator;

import javax.xml.soap.AttachmentPart;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;

import progress.message.msg.v26.Payload;

import com.bizmerce.ebms.cpa.Cpa;
import com.bizmerce.ebms.cpa.CpaProvider;
import com.bizmerce.ebms.engine.ebms.EBMSErrorList;
import com.bizmerce.ebms.engine.ebms.EBMSManifest;
import com.bizmerce.ebms.engine.ebms.EbmsError;
import com.bizmerce.ebms.engine.ebms.MessageHeader;
import com.bizmerce.ebms.engine.security.SecurityService;
import com.bizmerce.ebms.pref.Preferences;
import com.bizmerce.ebms.storage.DataRepository;

public class MessageProcessor {
	
	private CpaProvider cpaProvider;
	
	public void setCpaProvider(CpaProvider cpaProvider) {
		this.cpaProvider = cpaProvider;
	}

	/**
	 * TODO 1. MIMEHEADER에 따른 메시지 처리
	 * TODO 1.1 SOAP 메시지 처리
	 * TODO 2. XML 문서처리
	 * TODO 3. 첨부파일 처리 
	 * TODO 4. 응답메시지 리턴
	 * @param soapRequest
	 * @param soapResponse
	 * @throws FileNotFoundException 
	 */
	public SOAPMessage processReceiveMessage(SOAPMessage soapMessage) {
		DataRepository repository = new DataRepository();
		//SOAPMessage 정보에 대한 검증
		if(true) {
			//repository.registerMessage(soapMessage);
			try {
				SOAPPart soapPart = soapMessage.getSOAPPart();
				SOAPEnvelope soapEnvelope = soapPart.getEnvelope();

				MessageHeader messageHeader = new MessageHeader(soapEnvelope.getHeader());
				EBMSManifest messageBody = new EBMSManifest(soapEnvelope.getBody());
			
				Iterator<AttachmentPart> attachmentIterator = soapMessage.getAttachments();
				
				Preferences preferences = null;
				
				SecurityService securityService = SecurityService.newInstance();
				securityService.init(preferences.getSecurityConfig());
				
				int i=0;
				while(attachmentIterator.hasNext()) {
					AttachmentPart payload = attachmentIterator.next();
					if(i==0) {
						securityService.verificationAndDecryption(soapEnvelope.getHeader(), payload);
					} else {
						securityService.base64Decode(payload);
					}
				}
				/*
				EbmsErrorList errors = new EbmsErrorList();
				errors.add(new EbmsError());
				
				
				createAcknoledge(
				*/
			} catch (SOAPException e) {
				e.printStackTrace();
			}
			//Signature 
			//검증
			
			
			/*
			for (Payload payload : payloads) {
				repository.save(payload);
			}
			*/
		}
		return null;
	}
	
	public void processReceiveAckMessage(SOAPMessage ackMessage) {
		DataRepository repository = new DataRepository();
		if(true) {
			repository.registerMessage(ackMessage);
		}
	}
	
	/**
	 * TODO 1. 주어진 Cpa-Id를 기본으로 CpaProvider의 정보를 가지고 Soap메시지 구성
	 * TODO 2. XML 문서 담기
	 * TODO 3. 첨부파일 담기
	 * 아직 딱히 좋은 아이디어가 떠오르지 않는다.
	 * @param soapRequest
	 * @param soapResponse
	 */
	public SOAPMessage processSendMessage(String cpaId, Payload[] payloads) {
	//EbmsInProcessor, EbmsOutProcessor
		Cpa cpa = cpaProvider.getCpa(cpaId);
		SOAPMessage soapMessage = null;
		soapMessage = createSoapMessage(cpa, payloads);
		return soapMessage;
	}

	public SOAPMessage processSendAckMessage(String cpaId) {
		Cpa cpa = cpaProvider.getCpa(cpaId);
		SOAPMessage soapMessage = null;
		soapMessage = creatSoapMessage(cpa);
		return soapMessage;
	}

	private SOAPMessage creatSoapMessage(Cpa cpa) {
		return null;
	}

	private SOAPMessage createSoapMessage(Cpa cpa, Payload[] payloads) {
		return null;
	}
}
